package bdsimple.impl.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdsimple.abstractfactory.AbstractFactoryDAO;
import bdsimple.dao.base.ItemDAO;
import bdsimple.dao.base.ItemVampiroDAO;
import bdsimple.dao.base.VampiroDAO;
import bdsimple.dto.base.ItemDTO;
import bdsimple.dto.base.ItemVampiroDTO;
import bdsimple.dto.base.VampiroDTO;
import dao.base.api.IDTO;
import dao.base.impl.Reference;

public class ItemVampiroDAOImpl extends MySQLBaseDAO implements ItemVampiroDAO {

	public ItemVampiroDAOImpl() {
		super(ItemVampiroDTO.class);
	}

	@Override
	protected String createInsertCollst(IDTO dto) throws Exception {

		StringBuffer strbuf = new StringBuffer();

		strbuf.append(ItemVampiroDTOImpl.NIVEL_DESGASTE);
		strbuf.append(", ");
		strbuf.append(ItemVampiroDTOImpl.NIVEL_REPARACION);
		strbuf.append(", ");
		strbuf.append(ItemVampiroDTOImpl.EQUIPADO);
		strbuf.append(", ");
		strbuf.append(ItemVampiroDTOImpl.CANTIDAD);
		strbuf.append(", ");
		strbuf.append(ItemVampiroDTOImpl.VAMPIRO_REFERENCIA);
		strbuf.append(", ");
		strbuf.append(ItemVampiroDTOImpl.ITEM_REFERENCIA);

		return strbuf.toString();

	}

	@Override
	protected String createInsertValues(IDTO dto) throws Exception {

		ItemVampiroDTOImpl itemVampiroDTOImpl = (ItemVampiroDTOImpl) dto;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append(itemVampiroDTOImpl.getNivelDesgaste());
		strbuf.append(", ");
		strbuf.append(itemVampiroDTOImpl.getNivelReparacion());
		strbuf.append(", ");
		strbuf.append(itemVampiroDTOImpl.getEquipado());
		strbuf.append(", ");
		strbuf.append(itemVampiroDTOImpl.getCantidad());
		strbuf.append(", ");

		Reference<VampiroDTO> ref = itemVampiroDTOImpl.getVampiroDTORef();
		ref.checkInsert();
		strbuf.append(ref.getIdAsString());

		strbuf.append(", ");

		Reference<ItemDTO> ref1 = itemVampiroDTOImpl.getItemDTORef();
		ref1.checkInsert();
		strbuf.append(ref1.getIdAsString());

		return strbuf.toString();

	}

	@Override
	protected String createTableColumns() throws Exception {

		StringBuffer strbuf = new StringBuffer();

		VampiroDAO vampiroDAO = (VampiroDAO) //
		AbstractFactoryDAO.getFactoryDAO().getDAO(//
				VampiroDAO.class, connectionBean);

		ItemDAO itemDAO = (ItemDAO) //
		AbstractFactoryDAO.getFactoryDAO().getDAO(//
				ItemDAO.class, connectionBean);

		strbuf.append(ItemVampiroDTOImpl.ID);
		strbuf.append(" INT PRIMARY KEY AUTO_INCREMENT, ");
		strbuf.append(ItemVampiroDTOImpl.NIVEL_DESGASTE);
		strbuf.append(" INT, ");
		strbuf.append(ItemVampiroDTOImpl.NIVEL_REPARACION);
		strbuf.append(" INT, ");
		strbuf.append(ItemVampiroDTOImpl.EQUIPADO);
		strbuf.append(" BOOLEAN, ");
		strbuf.append(ItemVampiroDTOImpl.CANTIDAD);
		strbuf.append(" INT, ");
		strbuf.append(ItemVampiroDTOImpl.VAMPIRO_REFERENCIA);
		strbuf.append(" INT REFERENCES ");
		strbuf.append(vampiroDAO.getTableName());
		strbuf.append(", ");
		strbuf.append(ItemVampiroDTOImpl.ITEM_REFERENCIA);
		strbuf.append(" INT REFERENCES ");
		strbuf.append(itemDAO.getTableName());

		return strbuf.toString();

	}

	@Override
	protected String createUpdateValues(IDTO dto) throws Exception {

		ItemVampiroDTOImpl itemVampiroDTOImpl = (ItemVampiroDTOImpl) dto;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append(ItemVampiroDTOImpl.NIVEL_DESGASTE);
		strbuf.append(" = ");
		strbuf.append(itemVampiroDTOImpl.getNivelDesgaste());

		strbuf.append(", ");

		strbuf.append(ItemVampiroDTOImpl.NIVEL_REPARACION);
		strbuf.append(" = ");
		strbuf.append(itemVampiroDTOImpl.getNivelReparacion());

		strbuf.append(", ");

		strbuf.append(ItemVampiroDTOImpl.EQUIPADO);
		strbuf.append(" = ");
		strbuf.append(itemVampiroDTOImpl.getEquipado());

		strbuf.append(", ");

		strbuf.append(ItemVampiroDTOImpl.CANTIDAD);
		strbuf.append(" = ");
		strbuf.append(itemVampiroDTOImpl.getCantidad());

		strbuf.append(", ");

		strbuf.append(ItemVampiroDTOImpl.VAMPIRO_REFERENCIA);
		strbuf.append(" = ");

		Reference<VampiroDTO> ref = itemVampiroDTOImpl.getVampiroDTORef();
		ref.checkUpdate();
		strbuf.append(ref.getIdAsString());

		strbuf.append(", ");

		strbuf.append(ItemVampiroDTOImpl.ITEM_REFERENCIA);
		strbuf.append(" = ");

		Reference<ItemDTO> ref1 = itemVampiroDTOImpl.getItemDTORef();
		ref1.checkUpdate();
		strbuf.append(ref1.getIdAsString());

		return strbuf.toString();

	}

	@Override
	protected IDTO internalResultSetToDTO(ResultSet rs, IDTO dto)
			throws Exception {

		ItemVampiroDTOImpl ret = (ItemVampiroDTOImpl) dto;

		ret.setId(rs.getInt(ItemVampiroDTO.ID));
		
		ret.setNivelDesgaste(rs.getInt(ItemVampiroDTOImpl.NIVEL_DESGASTE));
		ret.setNivelReparacion(rs.getInt(ItemVampiroDTOImpl.NIVEL_REPARACION));
		ret.setEquipado(rs.getBoolean(ItemVampiroDTOImpl.EQUIPADO));
		ret.setCantidad(rs.getInt(ItemVampiroDTOImpl.CANTIDAD));

		Reference<VampiroDTO> ref = ret.getVampiroDTORef();
		ref.setRefIdent(rs.getInt(ItemVampiroDTOImpl.VAMPIRO_REFERENCIA));
		ref.setRefValue(null);

		Reference<ItemDTO> ref1 = ret.getItemDTORef();
		ref1.setRefIdent(rs.getInt(ItemVampiroDTOImpl.ITEM_REFERENCIA));
		ref1.setRefValue(null);

		return ret;

	}
	
	public List<ItemVampiroDTO> listByVampiroId(int vampiroId) throws SQLException, Exception {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT * FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(ItemVampiroDTO.VAMPIRO_REFERENCIA);
        strbuf.append(" = ");
        strbuf.append(vampiroId);

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        List<ItemVampiroDTO> ret = new ArrayList<ItemVampiroDTO>();

        while (rs.next()) {
            ret.add((ItemVampiroDTO) resultSetToDTO(rs));
        }

        return ret;
    }
	
	public ItemVampiroDTO loadByVampiroIdArticuloId(int vampiroId, int articuloId) throws SQLException, Exception {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT * FROM ");
        strbuf.append(getTableName());

        strbuf.append(" WHERE ");
        strbuf.append(ItemVampiroDTO.ITEM_REFERENCIA);
        strbuf.append(" = ");
        strbuf.append(articuloId);
        strbuf.append(" AND ");
        strbuf.append(ItemVampiroDTO.VAMPIRO_REFERENCIA);
        strbuf.append(" = ");
        strbuf.append(vampiroId);

        System.err.println(strbuf.toString());

        ResultSet rs = //
        connection.createStatement().executeQuery(strbuf.toString());

        if(rs.next()) {
            return (ItemVampiroDTO) resultSetToDTO(rs);
        }

        return null;
    }

}
